package com.study.zjh.smartrefreshdemo;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.study.zjh.smartrefreshdemo.activity.ScrollViewActivity;
import com.study.zjh.smartrefreshdemo.bean.ItemBean;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private List<ItemBean> list = new ArrayList<>();
    private MyAdapter  myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        refreshLayout=(RefreshLayout)findViewById(R.id.srl);
        recyclerView=(RecyclerView)findViewById(R.id.rv);
        initDate();
        initSmartRefresh();
    }

    private void initDate(){
        list = new ArrayList<ItemBean>();
        for (int i=0;i<20;i++){
            list.add(new ItemBean(
                    "initTitle"+i
            ));
        }
        myAdapter = new MyAdapter(list);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);//纵向线性布局
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(myAdapter);
        myAdapter.setOnClickItemListener(new MyAdapter.OnClickItemListener() {
            @Override
            public void onClickItemListener(int pos) {
                Toast.makeText(getApplication(), "数据点击"+list.get(pos).name, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this,ScrollViewActivity.class);startActivity(intent);            }
        });
    }


    private void initSmartRefresh() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //在这里执行上拉刷新时的具体操作(网络请求、更新UI等)

                //模拟网络请求到的数据
                ArrayList<ItemBean> newList = new ArrayList<ItemBean>();
                for (int i=0;i<20;i++){
                    newList.add(new ItemBean(
                            "newTitle+onRefresh"+i
                    ));
                }
                myAdapter.refresh(newList);
                refreshLayout.finishRefresh(2000/*,false*/);
                //不传时间则立即停止刷新    传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                if(list.size()<40){
                    //模拟网络请求到的数据
                    ArrayList<ItemBean> newList = new ArrayList<ItemBean>();
                    for (int i=0;i<20;i++){
                        newList.add(new ItemBean(
                                "addTitle+onLoadMore"+i
                        ));
                    }
                    myAdapter.add(newList);
                    //在这里执行下拉加载时的具体操作(网络请求、更新UI等)
                    refreshLayout.finishLoadMore(2000/*,false*/);//不传时间则立即停止刷新    传入false表示加载失败
                }else {
                    Toast.makeText(getApplication(), "数据全部加载完毕", Toast.LENGTH_SHORT).show();
                    refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
//                    refreshLayout.setNoMoreData(false);//恢复没有更多数据的原始状态 1.0.5
                }

            }
        });
    }

}
